package com.view.dialog;

import com.DAL.DatabaseConnection;
import com.view.component.AnnouncementComponent;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * 注册界面
 */

public class AddIncomeDialog extends JFrame {
    JLabel titleLabel = new JLabel("新建公告", JLabel.CENTER);

    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JLabel noticeLabel = new JLabel("公告标题：");
    JTextField noticeTxt = new JTextField();
    JLabel timeLabel = new JLabel("发布时间：");
    JTextField timeTxt = new JTextField();
    JLabel overtimeLabel = new JLabel("结束时间：");
    JTextField overtimeTxt = new JTextField();
    JLabel noticeContentLabel = new JLabel("公告内容：");
    JTextArea noticeContentTxt = new JTextArea(5, 15);
    JLabel metroLabel = new JLabel("备注：");
    JTextArea metroTxt = new JTextArea(3, 15);

    JScrollPane jScrollPane = new JScrollPane();

    JButton Confirm = new JButton(new AbstractAction("确认") {
        @Override
        public void actionPerformed(ActionEvent e) {
            String noticeTitle = noticeTxt.getText();
            String startTime = timeTxt.getText();
            String overTime = overtimeTxt.getText();
            String noticeContent = noticeContentTxt.getText();
            String metro = metroTxt.getText();
            String sql = "";
            Connection conn = DatabaseConnection.getConnection();
            if (noticeTitle.equals("") || startTime.equals("") || overTime.equals("") || noticeContent.equals("")) {
                JOptionPane.showMessageDialog(titleLabel, "输入的项不能为空", "警告", JOptionPane.WARNING_MESSAGE);
                return;
            }
            //从数据库获取结果集
            try {
                sql = "insert into announcement(title,time,overtime,content,metro)values(?,?,?,?,?)";  //定义要执行的sql语句 占位符?表示一个动态参数
                PreparedStatement preparedStatement = conn.prepareStatement(sql);
                preparedStatement.setString(1, noticeTitle);
                preparedStatement.setString(2, startTime);
                preparedStatement.setString(3, overTime);
                preparedStatement.setString(4, noticeContent);
                preparedStatement.setString(5, metro);
                preparedStatement.executeUpdate();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            AnnouncementComponent.request();
            AnnouncementComponent.tableModel.fireTableDataChanged();
            dispose();
        }
    });
    JButton Reset = new JButton(new AbstractAction("重置") {
        @Override
        public void actionPerformed(ActionEvent e) {
            noticeTxt.setText("");
            timeTxt.setText("");
            overtimeTxt.setText("");
            noticeContentTxt.setText("");
            metroTxt.setText("");
        }
    });
    JButton Back = new JButton(new AbstractAction("返回") {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });

    JLabel RemindBar = new JLabel("温馨提醒：想不到提醒你什么，但就是不想删这个控件，就是玩");

    public AddIncomeDialog() {
        //TODO：将每个窗口的标题提取至一处
        super("添加事件");
        Container contentPane = getContentPane();

        //设置字体
        setFont();

        //自动换行
        noticeContentTxt.setLineWrap(true);
        metroTxt.setLineWrap(true);

        //把组件加入面板
        contentPane.add(titleLabel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        centerPanel.add(noticeLabel);
        centerPanel.add(noticeTxt);
        centerPanel.add(timeLabel);
        centerPanel.add(timeTxt);
        centerPanel.add(overtimeLabel);
        centerPanel.add(overtimeTxt);
        centerPanel.add(noticeContentLabel);
        centerPanel.add(noticeContentTxt);
        centerPanel.add(metroLabel);
        centerPanel.add(metroTxt);
        centerPanel.add(Confirm);
        centerPanel.add(Reset);
        centerPanel.add(Back);

        //日历基于弹簧布局兼容性无法使用
//        DateChooserUtils dateChooserUtils = DateChooserUtils.getInstance();
//        dateChooserUtils.register(timeTxt);
//        dateChooserUtils.register(overtimeTxt);

        //TODO:滚动条、为什么会出现文本框半边框

        contentPane.add(RemindBar, BorderLayout.SOUTH);

        //设置弹簧布局
        springLayout(contentPane);

        //设置窗口图标
        setIcon();

        setSize(600, 800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }


    //字体及大小设置 Method

    private void setFont() {
        titleLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        titleLabel.setPreferredSize(new Dimension(0, 140));

        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        noticeLabel.setFont(centerFont);
        noticeTxt.setPreferredSize(new Dimension(200, 30));
        timeLabel.setFont(centerFont);
        timeTxt.setPreferredSize(new Dimension(200, 30));
        overtimeLabel.setFont(centerFont);
        overtimeTxt.setPreferredSize(new Dimension(200, 30));
        noticeContentLabel.setFont(centerFont);
        noticeContentTxt.setPreferredSize(new Dimension(200, 90));
        metroLabel.setFont(centerFont);
        metroTxt.setPreferredSize(new Dimension(200, 60));
        Confirm.setFont(centerFont);
        Reset.setFont(centerFont);
        Back.setFont(centerFont);

        RemindBar.setFont(centerFont);
        RemindBar.setBorder(new CompoundBorder(new LineBorder(Color.WHITE),
                new EmptyBorder(4, 4, 4, 4)));
    }
    //弹簧布局 Method

    private void springLayout(Container contentPane) {
        //布局noticeLabel
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(noticeLabel), Spring.width(noticeTxt)),
                Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;

        springLayout.putConstraint(SpringLayout.WEST, noticeLabel, -offsetX,
                SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, noticeLabel, 20, SpringLayout.NORTH, centerPanel);
        //布局noticeTxt
        springLayout.putConstraint(SpringLayout.WEST, noticeTxt, 20, SpringLayout.EAST, noticeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, noticeTxt, 0, SpringLayout.NORTH, noticeLabel);
        //布局timeLabel
        springLayout.putConstraint(SpringLayout.EAST, timeLabel, 0, SpringLayout.EAST, noticeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, timeLabel, 40, SpringLayout.SOUTH, noticeLabel);
        //布局timeTxt
        springLayout.putConstraint(SpringLayout.WEST, timeTxt, 20, SpringLayout.EAST, timeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, timeTxt, 0, SpringLayout.NORTH, timeLabel);
        //布局overtimeLabel
        springLayout.putConstraint(SpringLayout.EAST, overtimeLabel, 0, SpringLayout.EAST, noticeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, overtimeLabel, 40, SpringLayout.SOUTH, timeLabel);
        //布局overtimeTxt
        springLayout.putConstraint(SpringLayout.WEST, overtimeTxt, 20, SpringLayout.EAST, overtimeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, overtimeTxt, 0, SpringLayout.NORTH, overtimeLabel);
        //布局noticeContentLabel
        springLayout.putConstraint(SpringLayout.EAST, noticeContentLabel, 0, SpringLayout.EAST, overtimeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, noticeContentLabel, 40, SpringLayout.SOUTH, overtimeLabel);
        //布局noticeContentTxt
        springLayout.putConstraint(SpringLayout.WEST, noticeContentTxt, 20, SpringLayout.EAST, noticeContentLabel);
        springLayout.putConstraint(SpringLayout.NORTH, noticeContentTxt, 0, SpringLayout.NORTH, noticeContentLabel);
        //布局metroLabel
        springLayout.putConstraint(SpringLayout.EAST, metroLabel, 0, SpringLayout.EAST, noticeContentLabel);
        springLayout.putConstraint(SpringLayout.NORTH, metroLabel, 120, SpringLayout.NORTH, noticeContentTxt);
        //布局metroTxt
        springLayout.putConstraint(SpringLayout.WEST, metroTxt, 20, SpringLayout.EAST, metroLabel);
        springLayout.putConstraint(SpringLayout.NORTH, metroTxt, 0, SpringLayout.NORTH, metroLabel);
        //布局Confirm
        springLayout.putConstraint(SpringLayout.EAST, Confirm, -60, SpringLayout.EAST, timeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, Confirm, 350, SpringLayout.SOUTH, timeLabel);
        //布局Reset
        springLayout.putConstraint(SpringLayout.EAST, Reset, 160, SpringLayout.EAST, Confirm);
        springLayout.putConstraint(SpringLayout.NORTH, Reset, 0, SpringLayout.NORTH, Confirm);
        //布局Back
        springLayout.putConstraint(SpringLayout.EAST, Back, 160, SpringLayout.EAST, Reset);
        springLayout.putConstraint(SpringLayout.NORTH, Back, 0, SpringLayout.NORTH, Confirm);

        SpringLayout.Constraints titleLabelC = springLayout.getConstraints(titleLabel);
//        titleLabelC.setX(Spring.constant(100));
        titleLabelC.setY(Spring.constant(50));

    }
    //设置窗体图标 Method

    private void setIcon() {
//        URL resource = FirstScene.class.getClassLoader().getResource("Icon.jpg");
//        Image image = new ImageIcon(resource).getImage();
//        setIconImage(image);
        Image icon = Toolkit.getDefaultToolkit().getImage("src/resource/Icon.jpg");
        this.setIconImage(icon);
    }
    //Main方法

    public static void main(String[] args) {
        new com.view.dialog.AddIncomeDialog();
    }

}
